﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;
using Activity.Model;
using Newtonsoft.Json;
using Nancy.Extensions;

namespace Activity.API
{
    public class ShopPermissionsHandler
    {
        public IList<t_task_shop_permissions> GetShopPermissionsList(string ProvinceName, string CityName,
            string ShopStr, string PartName)
        {
            try
            {
                var db = new ActivityEntities();
                var list = db.t_task_shop_permissions
                    .Where(d => (string.IsNullOrEmpty(ProvinceName) || d.ProvinceName == ProvinceName) &&
                                (string.IsNullOrEmpty(CityName) || d.AreaName == CityName) &&
                                (string.IsNullOrEmpty(ShopStr) || d.ShopCode.Contains(ShopStr) ||
                                 d.ShopName.Contains(ShopStr)) &&
                                (string.IsNullOrEmpty(PartName) || d.Display == PartName))
                    .OrderBy(d => d.ShopCode);
                return list.ToList();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }

        public int ImportShopPermissionsList(string JsonData)
        {
            try
            {
                const string sql = "DELETE FROM [dbo].[t_task_shop_permissions] WHERE ShopCode IN({0})";
                var db = new ActivityEntities();
                var list = JsonConvert.DeserializeObject<IList<t_task_shop_permissions>>(JsonData).ToList();
                var shopCodeList = list.Select(d => "'" + d.ShopCode + "'").ToList();
                db.Database.ExecuteSqlCommand(string.Format(sql, string.Join(",", shopCodeList)));
                list.ForEach(d =>
                {
                    d.Creator = com.km.library.UserInfo.UserID.ToString();
                    d.CreateDate = DateTime.Now;
                });
                foreach (var item in list)
                {
                    db.t_task_shop_permissions.Add(item);
                }
                db.SaveChanges();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return -1;
            }
        }

        public IList<string> GetDisplayList()
        {
            try
            {
                var db = new ActivityEntities();
                var list = db.t_task_shop_permissions.Where(d => d.Display != null).OrderBy(d => d.Display)
                    .Select(d => d.Display).DistinctBy(d => d).ToList();
                return list;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }

        public int DeleteShopPermissions(string JsonData)
        {
            try
            {
                const string sql = "DELETE FROM [dbo].[t_task_shop_permissions] WHERE ID IN({0})";
                var list = JsonConvert.DeserializeObject<IList<int>>(JsonData);
                var db = new ActivityEntities();
                db.Database.ExecuteSqlCommand(string.Format(sql, string.Join(",", list)));
                db.SaveChanges();
                return 0;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return -1;
            }
        }

        public IList<ShopAreaModel> GetShopAreaList()
        {
            try
            {
                var db = new ActivityEntities();
                var list = db.t_task_shop_permissions.Select(d => new {d.AreaName, d.ProvinceName})
                    .DistinctBy(d => new {d.AreaName, d.ProvinceName}).ToList();
                var newList = list.DistinctBy(d => d.ProvinceName).Select(d => new ShopAreaModel
                {
                    AreaName = d.ProvinceName,
                    child = list.Where(r => r.ProvinceName == d.ProvinceName).Select(r => new ShopAreaModel
                    {
                        AreaName = r.AreaName
                    }).ToList()
                }).ToList();
                return newList;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }
    }

    public class ShopAreaModel
    {
        public string AreaName { get; set; }
        public IList<ShopAreaModel> child { get; set; }
    }
}
